package com.mutouren.modules.org.model;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;

public class Function {
	
	public static final Integer ROOT_ID = 0;
	public static final String ROOT_NAME = "功能菜单";
	public static final String PATH_SEPARATOR = ",";	
	
	private Integer funcId;
	private Integer appId;
	private String funcName;
	private String url;
	private Boolean isMenu;
	private Integer layoutCode;
	private String extraParam;
	private Integer parentId;
	private String parentIdPath;
	private Integer sequence;
	@JsonIgnore
	private String description;
	@JsonIgnore
	private ValidState validState;
	@JsonIgnore
	private Date createTime;
	@JsonIgnore
	private Date updateTime;
	
	@JsonIgnore
	private String parentFuncName; 
	
	public Function() {
		this.validState = ValidState.Normal;
		this.isMenu = true;		
	}
	
	public static Function createRoot() {		
		Function result = new Function();
		result.funcId = ROOT_ID;
		result.funcName = ROOT_NAME;
		result.parentIdPath = "";
		result.parentId = -1;
		
		return result;
	}
	
	public boolean isRoot() {
		return this.getFuncId().equals(ROOT_ID);
	}	
	
	public Integer getFuncId() {
		return funcId;
	}
	public void setFuncId(Integer funcId) {
		this.funcId = funcId;
	}
	public Integer getAppId() {
		return appId;
	}
	public void setAppId(Integer appId) {
		this.appId = appId;
	}
	public String getFuncName() {
		return funcName;
	}
	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Boolean getIsMenu() {
		return isMenu;
	}
	public void setIsMenu(Boolean isMenu) {
		this.isMenu = isMenu;
	}
	public Integer getLayoutCode() {
		return layoutCode;
	}
	public void setLayoutCode(Integer layoutCode) {
		this.layoutCode = layoutCode;
	}
	public String getExtraParam() {
		return extraParam;
	}
	public void setExtraParam(String extraParam) {
		this.extraParam = extraParam;
	}
	public Integer getParentId() {
		return parentId;
	}
	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}
	public String getParentIdPath() {
		return parentIdPath;
	}
	public void setParentIdPath(String parentIdPath) {
		this.parentIdPath = parentIdPath;
	}
	public Integer getSequence() {
		return sequence;
	}
	public void setSequence(Integer sequence) {
		this.sequence = sequence;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public ValidState getValidState() {
		return validState;
	}
	public void setValidState(ValidState validState) {
		this.validState = validState;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}	
	
	public String getSelfIdPath() {
		return buildSelfIdPath();
	}	
	
	public String buildSelfIdPath() {
		if (this.getFuncId().equals(ROOT_ID)) {
			return String.format("%d%s", ROOT_ID, PATH_SEPARATOR);
		} else {
			if (StringUtils.isNullOrEmpty(this.getParentIdPath())) {
				throw new IllegalArgumentException(
						"parentOrg.getParentIdPath() is null or empty");
			}
			return String.format("%s%d%s", this.getParentIdPath(), this.getFuncId(), PATH_SEPARATOR);
		}
	}

	public String getParentFuncName() {
		return parentFuncName;
	}

	public void setParentFuncName(String parentFuncName) {
		this.parentFuncName = parentFuncName;
	}		
}
